package com.kouxin.appaccount.developer;

import org.apache.struts2.dispatcher.SessionMap;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.opensymphony.xwork2.util.logging.Logger;
import com.opensymphony.xwork2.util.logging.LoggerFactory;

public class DeveloperAuthenticationInterceptor implements Interceptor {

	private static final long serialVersionUID = -5231650204615291619L;
	private static final Logger LOG = LoggerFactory.getLogger(DeveloperAuthenticationInterceptor.class);
	public static final String DEVELOPER_SESSION_KEY = "developerSessionKey";

	@Override
	public void destroy() {
	}

	@Override
	public void init() {
	}

	@SuppressWarnings("unchecked")
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		LOG.debug("Authenticating developer");

		SessionMap session = (SessionMap) ActionContext.getContext().get(ActionContext.SESSION);
        Developer developer = (Developer) session.get(DEVELOPER_SESSION_KEY);

        if (developer == null) {
            return Action.LOGIN;
        }
        return invocation.invoke();
	}

}
